<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>{{c.gradeLevel}}{{c.name}} 班主任:{{c.teacherName}}</h1>
    <input type="text" placeholder="姓名" v-model="s.name">
    <input type="text" placeholder="年龄" v-model="s.age">
    <input type="text" placeholder="性别" v-model="s.gender">
    <input type="button" value="添加学生" @click="insert()">
    <table border="2">
        <caption>学生列表</caption>
        <tr><td>序号</td><td>名字</td><td>年龄</td><td>性别</td><td>操作</td></tr>
        <tr v-for="(stu,i) in arr">
            <td>{{i+1}}</td><td>{{stu.name}}</td><td>{{stu.age}}</td>
            <td>{{stu.gender}}</td>
            <td><a href="javascript:void(0)" @click="del(stu.id)">删除</a></td>
        </tr>
    </table>
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.6.14/vue.min.js"></script>
<script>
    let v = new Vue({
        el:"body>div",
        data:{
            c:{name:"",teacherName: "",gradeLevel: ""},
            s:{name:"",age:"",gender:"",classId:""},
            arr:[]
        },
        methods:{//增删改用post  ,如果有参数则要加上@requesbady在后端处理
            del(id){
                if (confirm("你确定要删除此学生吗？")){
                    axios.post("/student/delete?id="+id).then(function (response) {
                        location.reload();
                    })
                }
            },
            //从地址栏中取出id的值 并赋值给学生的对象
            insert(){
              v.s.classId=new URLSearchParams(location.search).get("id");
              axios.post("/student/insert",v.s).then(function () {
                  location.reload();
              })
            }
        },
        created:function () {//查询用get
            //通过id查询班级详情
            axios.get("/class/selectById"+location.search).then(function (response) {
                v.c = response.data;
            })
            //通过班级id查询学生
            axios.get("/student/selectByCid"+location.search).then(function (response) {
                v.arr = response.data;
            })
        }
    })
</script>
</body>
</html>